package cn.neusoft.controller;

import cn.neusoft.pojo.Address;
import cn.neusoft.pojo.CartItem;
import cn.neusoft.pojo.Order;
import cn.neusoft.pojo.User;
import cn.neusoft.query.BusinessQuery;
import cn.neusoft.service.*;
import cn.neusoft.vo.ResultBean;
import org.omg.PortableInterceptor.Interceptor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;

@RestController
@RequestMapping("/app")
public class AppController {

    @Autowired
    IAppService appService;

    @Autowired
    IBusinessService businessService;

    @Autowired
    IFoodService foodService;

    @Autowired
    ICartItemService cartItemService;

    @Autowired
    IOrderService orderService;

    @Autowired
    IAddressService addressService;

    @Autowired
    IUserService userService;

    //查询推荐商家
    @GetMapping("/business/recommend")   //   get  +  /app/business/recommend
    public ResultBean getRecommendBusiness(){
        System.out.println("-------AppController-----getRecommendBusiness--------");
        return appService.getRecommendBusiness();
    }

    @PostMapping("/business/page")   // post  /app/business/page
    public ResultBean getByPage(@RequestBody BusinessQuery businessQuery){
        System.out.println("-------AppController--------getByPage-------------");
        System.out.println(businessQuery);
        return businessService.getByPage(businessQuery);
    }

    /*
    根据商家编号查询该商家的所有餐品
     */
    @GetMapping("/food/business/{bid}")    //  get  /app/food/business/1
    public ResultBean getFoodByBid(@PathVariable("bid") Integer businessId){
        System.out.println("---------AppController--------getFoodByBid-----------");
        System.out.println(businessId);
        return foodService.getByBusinessId(businessId);
    }

    //  get   /business/1004
    @GetMapping("/business/{id}")   // /app/business/1003
    public ResultBean getBusinessById(@PathVariable("id") Integer id){
        System.out.println("-------AppController--------getBusinessById-------------");
        System.out.println(id);
        return businessService.getById(id);
    }

    @PostMapping("/cart/save")     //  请求方式 + 请求路径   post  /app/cart/save
    public ResultBean saveCartItem(@RequestBody CartItem cartItem){
        System.out.println("----AppController-----save--------");
        System.out.println(cartItem);
        if (cartItem.getCartId()==null){
            return cartItemService.add(cartItem);
        }
        return cartItemService.update(cartItem);
    }

    /*
     某个用户在某个商家的购物车内容
     传递的请求参数： userId  businessId
     */
    @GetMapping("/cart/{userId}/{businessId}")
    public ResultBean getCartByBidAndUid(@PathVariable("userId") Integer userId,
                                         @PathVariable("businessId") Integer businessId){
        System.out.println("------AppController---------getCartByBidAndUid----------------");
        return cartItemService.getCartByBidAndUid(userId,businessId);
    }

    //  delete   /cartItem/1003
    @DeleteMapping("/cartItem/{id}")   // delete + /app/cartItem/1003
    public ResultBean delCartItem(@PathVariable("id") Integer id){
        System.out.println("-------AppController--------del-------------");
        System.out.println(id);
        return cartItemService.delete(id);
    }

    @PostMapping("/order/add")     //  请求方式 + 请求路径   post  /app/order/add
    public ResultBean addOrder(@RequestBody Order order){
        System.out.println("----AppController-----add--------");
        System.out.println(order);
        return orderService.add(order);
    }

    @PutMapping("/order/update")    //   put   /app/order/update
    public ResultBean updateOrder(@RequestBody Order order){
        System.out.println("-------AppController--------update-------------");
        System.out.println(order);
        return orderService.update(order);
    }

    @GetMapping("/address/user/{userId}")    //   get +  /app/address/user/1
    public ResultBean getAddressByUserId(@PathVariable("userId") Integer userId){
        System.out.println("--------AppController--------getAddressByUserId---------");
        System.out.println(userId);
        return addressService.getByUserId(userId);
    }

    @PostMapping("/address/save")      //  请求方式 + 请求路径   post  /app/address/save
    public ResultBean addressSave(@RequestBody Address address){
        System.out.println("----AppController-----save--------");
        System.out.println(address);
        if (address.getAddressId()==null){
            return addressService.add(address);
        }
        return addressService.update(address);
    }

    //  get   /address/1004
    @GetMapping("/address/{id}")   // get /app/address/1003
    public ResultBean getAddressById(@PathVariable("id") Integer id){
        System.out.println("-------AppController--------getById-------------");
        System.out.println(id);
        return addressService.getById(id);
    }

    @GetMapping("/order/user/{userId}")   //  get  +   /app/order/user/1
    public ResultBean getOrderByUserId(@PathVariable("userId") Integer userId){
        System.out.println("-----AppController------getOrderByUserId--------");
        System.out.println(userId);
        return orderService.getOrderByUserId(userId);
    }

    /**
     * 通过订单编号查询订单详情
     * @param orderId
     * @return
     */
    @GetMapping("/order/detail/{orderId}")   //get + /app/order/detail/1
    public ResultBean getDetailByOrderId(@PathVariable("orderId") Integer orderId){
        System.out.println("-----AppController------getByOrderId------");
        System.out.println(orderId);
        return orderService.getDetailsByOrderId(orderId);
    }

    @PostMapping("/login")
    public ResultBean login(HttpSession session, @RequestBody User user){
        System.out.println("-------AppController-----login-----");
        System.out.println(user.getUsername()+"  "+user.getPassword());
        ResultBean resultBean =  userService.login(user.getUsername(),user.getPassword());
        if (resultBean.getCode()==200){
            System.out.println("*****用户登录成功******"+session.getId());
            //将登录成功的用户存储到session中
            session.setAttribute("loginUser",user);
        }
        return resultBean;
    }

}
